﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using QLChoDiaOc.Process;

namespace QLChoDiaOc.Forms
{
    /// <summary>
    /// Interaction logic for DichVuQuangCao.xaml
    /// </summary>
    public partial class DichVuQuangCao : Page
    {
        InsertInfo _insertInfo = new InsertInfo();
        SelectInfo _selectInfo = new SelectInfo();
        OtherHandle _otherHandle = new OtherHandle();
        GetID _getId = new GetID();

        static bool m_bSelectDataBang = false;
        static bool m_bSelectDataToBuom = false;

        public DichVuQuangCao()
        {
            InitializeComponent();
            HienThiThongTin();
        }

        void HienThiThongTin()
        {
            if (App.IsDangKyQuangCao == true)
            {
                GroupThongTinChung.IsEnabled = false;
                GroupQCBang.IsEnabled = false;
                GroupQCBao.IsEnabled = false;
                GroupQCToBuom.IsEnabled = false;
                btnChapNhanAll.Visibility = Visibility.Collapsed;
                btnHoanTatAll.Visibility = Visibility.Collapsed;

                tenLoaiBangComboBox.Items.Clear();
                for (int i = 0; i < _selectInfo.SelectAllTenLoaiBang().Count; i++)
                    tenLoaiBangComboBox.Items.Add(_selectInfo.SelectAllTenLoaiBang()[i]);

                toBaoComboBox.Items.Clear();
                for (int i = 0; i < _selectInfo.SelectAllTenToBao().Count; i++)
                    toBaoComboBox.Items.Add(_selectInfo.SelectAllTenToBao()[i]);

                if (App.IsChapNhanQCBang == true)
                {
                    maQCBangTextBox.Text = App.QcBangTemp.MaQCBang;
                    tenLoaiBangComboBox.SelectedItem = _selectInfo.SelectTenLoaiBang(App.QcBangTemp.MaLoaiBang);
                    donGiaBangTextBox.Text = _selectInfo.SelectDonGiaLoaiBang(_selectInfo.SelectTenLoaiBang(App.QcBangTemp.MaLoaiBang)).ToString();
                    thanhTienBangTextBox.Text = App.QcBangTemp.ThanhTien.ToString();
                    btnChapNhanBang.Visibility = Visibility.Collapsed;
                }

                if (App.IsChapNhanQCBao == true)
                {
                    maQCBaoTextBox.Text = App.QcBaoTemp.MaQCBao;
                    toBaoComboBox.SelectedItem = _selectInfo.SelectTenToBao(App.QcBaoTemp.MaToBao);
                    if (App.QcBaoTemp.CoHinhAnh == 1)
                        dkChupAnhcheckBox.IsChecked = true;
                    else dkChupAnhcheckBox.IsChecked = false;
                    kyBatDauTextBox.Text = App.QcBaoTemp.KyBatDau.ToString();
                    soKyTextBox.Text = App.QcBaoTemp.SoKy.ToString();
                    thanhTienBaoTextBox.Text = App.QcBaoTemp.ThanhTien.ToString();
                    btnChapNhanBao.Visibility = Visibility.Collapsed;
                }

                if (App.IsChapNhanQCToBuom == true)
                {
                    maQCToBuomTextBox.Text = App.QcToBuomTemp.MaQCToBuom;
                    soLuongTextBox.Text = App.QcToBuomTemp.SoLuong.ToString();
                    donGiaToBuomTextBox.Text = App.QcToBuomTemp.DonGia.ToString();
                    thanhTienToBuomTextBox.Text = App.QcToBuomTemp.ThanhTien.ToString();
                    btnChapNhanToBuom.Visibility = Visibility.Collapsed;
                    btnTinhTienToBuom.Visibility = Visibility.Collapsed;
                }
            }
        }

        void ResetQCBao()
        {
            GroupQCBao.IsEnabled = false;
            toBaoComboBox.Items.Clear();
            dkChupAnhcheckBox.IsChecked = false;
            toBaoComboBox.SelectedItem = null;
            maQCBaoTextBox.Text = "";
            thanhTienBaoTextBox.Text = "";
            soKyTextBox.Text = "";
            kyBatDauTextBox.Text = "";
        }

        void ResetQCToBuom()
        {
            GroupQCToBuom.IsEnabled = false;
            maQCToBuomTextBox.Text = "";
            soLuongTextBox.Text = "";
            donGiaToBuomTextBox.Text = "";
            thanhTienToBuomTextBox.Text = "";
        }

        void ResetQCBang()
        {
            GroupQCBang.IsEnabled = false;
            tenLoaiBangComboBox.SelectedItem = null;
            tenLoaiBangComboBox.Items.Clear();
            maQCBangTextBox.Text = "";
            donGiaBangTextBox.Text = "";
            thanhTienBangTextBox.Text = "";
        }

        void ResetAll()
        {
            ResetQCBang();
            ResetQCBao();
            ResetQCToBuom();
            qcBang_checkbox.IsChecked = false;
            qcBao_checkbox.IsChecked = false;
            qcToBuom_checkbox.IsChecked = false;
            tongTienTextBox.Text = "";
            GroupTongTien.Visibility = Visibility.Collapsed;
            btnChapNhanAll.IsEnabled = false;
            btnHoanTatAll.Visibility = Visibility.Visible;
            GroupThongTinChung.IsEnabled = true;
        }

        private void qcBao_checkbox_Click(object sender, RoutedEventArgs e)
        {
            if (qcBao_checkbox.IsChecked == true)
            {
                GroupQCBao.IsEnabled = true;

                toBaoComboBox.Items.Clear();
                for (int i = 0; i < _selectInfo.SelectAllTenToBao().Count; i++)
                    toBaoComboBox.Items.Add(_selectInfo.SelectAllTenToBao()[i]);

                maQCBaoTextBox.Text = _getId.GenerateMaQCBao();
            }
            else
                ResetQCBao();
        }

        private void qcToBuom_checkbox_Click(object sender, RoutedEventArgs e)
        {
            if (qcToBuom_checkbox.IsChecked == true)
            {
                GroupQCToBuom.IsEnabled = true;

                maQCToBuomTextBox.Text = _getId.GenerateMaQCToBuom();
            }
            else
                ResetQCToBuom();
        }

        private void qcBang_checkbox_Click(object sender, RoutedEventArgs e)
        {
            if (qcBang_checkbox.IsChecked == true)
            {
                GroupQCBang.IsEnabled = true;
                tenLoaiBangComboBox.Items.Clear();
                for (int i = 0; i < _selectInfo.SelectAllTenLoaiBang().Count; i++)
                    tenLoaiBangComboBox.Items.Add(_selectInfo.SelectAllTenLoaiBang()[i]);
                maQCBangTextBox.Text = _getId.GenerateMaQCBang();
            }
            else
                ResetQCBang();
        }

        private void btnChapNhanToBuom_Click(object sender, RoutedEventArgs e)
        {
            if (thanhTienToBuomTextBox.Text == "")
                MessageBox.Show("Nhấn nút Tính Tiền trước", "Thông Báo");
            else
            {
                try
                {
                    App.QcToBuomTemp.MaQCToBuom = maQCToBuomTextBox.Text;
                    App.QcToBuomTemp.SoLuong = int.Parse(soLuongTextBox.Text.ToString());
                    App.QcToBuomTemp.DonGia = int.Parse(donGiaToBuomTextBox.Text);
                    App.QcToBuomTemp.ThanhTien = int.Parse(thanhTienToBuomTextBox.Text);
                    App.IsChapNhanQCToBuom = true;
                    MessageBox.Show("Thông tin hợp lệ", "Thông Báo");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Thông Báo");
                }
            }
        }

        private void btnChapNhanBang_Click(object sender, RoutedEventArgs e)
        {
            if (thanhTienBangTextBox.Text == "")
                MessageBox.Show("Chưa chọn loại bảng", "Thông Báo");
            else
            {
                try
                {
                    App.QcBangTemp.MaQCBang = maQCBangTextBox.Text;
                    App.QcBangTemp.MaLoaiBang = _selectInfo.SelectMaLoaiBang(tenLoaiBangComboBox.SelectedItem.ToString());
                    App.QcBangTemp.ThanhTien = int.Parse(thanhTienBangTextBox.Text);
                    App.QcBangTemp.TinhTrang = 1;
                    App.IsChapNhanQCBang = true;
                    MessageBox.Show("Thông tin hợp lệ", "Thông Báo");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Thông Báo");
                }
            }
        }

        private void btnChapNhanBao_Click(object sender, RoutedEventArgs e)
        {
            if (thanhTienBaoTextBox.Text == "")
                MessageBox.Show("Chưa Nhập Đầy Đủ Thông Tin", "Thông Báo");
            else
            {
                try
                {
                    App.QcBaoTemp.MaQCBao = maQCBaoTextBox.Text;
                    App.QcBaoTemp.MaToBao = _selectInfo.SelectMaToBao(toBaoComboBox.SelectedItem.ToString());
                    App.QcBaoTemp.ThanhTien = int.Parse(thanhTienBaoTextBox.Text);
                    App.QcBaoTemp.TinhTrang = 1;
                    App.QcBaoTemp.SoKy = int.Parse(soKyTextBox.Text);
                    App.QcBaoTemp.KyBatDau = int.Parse(kyBatDauTextBox.Text);
                    App.IsChapNhanQCBao = true;
                    if (dkChupAnhcheckBox.IsChecked == true)
                    {
                        App.QcBaoTemp.CoHinhAnh = 1;
                        App.IsChupAnh = true;
                    }
                    else
                    {
                        App.QcBaoTemp.CoHinhAnh = 0;
                        App.IsChupAnh = false;
                    }
                    MessageBox.Show("Thông tin hợp lệ", "Thông Báo");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Thông Báo");
                }
            }
        }

        private void tenLoaiBangComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (tenLoaiBangComboBox.SelectedItem != null)
            {
                if (!m_bSelectDataBang)
                {
                    _otherHandle.TinhTienQCBang(tenLoaiBangComboBox.SelectedItem.ToString(), ref donGiaBangTextBox, ref thanhTienBangTextBox);
                }
                else
                    m_bSelectDataBang = false;
            }
        }

        //private void soLuongComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        //{
        //    if (soLuongComboBox.SelectedItem != null)
        //    {
        //        try
        //        {
        //            if (!m_bSelectDataToBuom)
        //            {
        //                int _iSoluong = int.Parse(soLuongComboBox.SelectedItem.ToString());

        //                _otherHandle.TinhTienQCToBuom(_iSoluong, ref donGiaToBuomTextBox, ref thanhTienToBuomTextBox);
        //            }
        //            else
        //                m_bSelectDataToBuom = false;
        //        }
        //        catch (Exception ex)
        //        {
        //            MessageBox.Show(ex.Message, "Thông Báo");
        //        }
        //    }
        //}

        //private void btnMaQCBaoAuto_Click(object sender, RoutedEventArgs e)
        //{
        //    maQCBaoTextBox.Text = _getId.GenerateMaQCBao();
        //}

        //private void btnMaQCToBuomAuto_Click(object sender, RoutedEventArgs e)
        //{
        //    maQCToBuomTextBox.Text = _getId.GenerateMaQCToBuom();
        //}

        //private void btnMaQCBangAuto_Click(object sender, RoutedEventArgs e)
        //{
        //    maQCBangTextBox.Text = _getId.GenerateMaQCBang();
        //}

        private void btnTinhTongCong_Click(object sender, RoutedEventArgs e)
        {
            _otherHandle.TinhTongTien(thanhTienBaoTextBox, thanhTienToBuomTextBox, thanhTienBangTextBox, ref tongTienTextBox);
            btnChapNhanAll.IsEnabled = true;
            btnTinhTongCong.IsEnabled = false;
        }

        private void btnHoanTatAll_Click(object sender, RoutedEventArgs e)
        {
            if (App.IsChapNhanQCBao != true)
            {
                toBaoComboBox.SelectedItem = null;
                dkChupAnhcheckBox.IsChecked = false;
                kyBatDauTextBox.Text = "";
                soKyTextBox.Text = "";
                thanhTienBaoTextBox.Text = "0";
            }
            if (App.IsChapNhanQCBang != true)
            {
                thanhTienBangTextBox.Text = "0";
                donGiaBangTextBox.Text = "";
                tenLoaiBangComboBox.SelectedItem = null;
            }
            if (App.IsChapNhanQCToBuom != true)
            {
                thanhTienToBuomTextBox.Text = "0";
                donGiaToBuomTextBox.Text = "";
                //soLuongComboBox.SelectedItem = null;
                soLuongTextBox.Text = "";
            }

            GroupTongTien.Visibility = Visibility.Visible;
            GroupThongTinChung.IsEnabled = false;
            GroupQCBang.IsEnabled = false;
            GroupQCBao.IsEnabled = false;
            GroupQCToBuom.IsEnabled = false;
            btnHoanTatAll.Visibility = Visibility.Collapsed;
        }

        private void soKyTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            _otherHandle.IsNumber(soKyTextBox);
            if (toBaoComboBox.SelectedItem != null)
                _otherHandle.TinhTienQCBao(toBaoComboBox.SelectedItem.ToString(), soKyTextBox, ref thanhTienBaoTextBox);
        }

        private void btnChapNhanAll_Click(object sender, RoutedEventArgs e)
        {
            btnChapNhanAll.Visibility = Visibility.Collapsed;
            if (App.QcBaoTemp.CoHinhAnh == 1 && App.IsChapNhanQCBao == true)
                App.IsChupAnh = true;
            else
                App.IsChupAnh = false;
            WinPhieuDangKy m_wPhieuDangKy = new WinPhieuDangKy();
            m_wPhieuDangKy.Show();
            App.IsDangKyQuangCao = true;
            App.Mainform.btnXacNhanGiayTo.IsEnabled = true;
            App.PhieuThuTemp.SoTien = int.Parse(tongTienTextBox.Text);
        }

        //private void HienThiThongTin()
        //{
        //    if (App.IsDangKyQuangCao == true)
        //    {
        //        GroupThongTinChung.Visibility = Visibility.Collapsed;
        //        btnChapNhanAll.Visibility = Visibility.Collapsed;
        //        btnHoanTatAll.Visibility = Visibility.Collapsed;
        //        GroupTongTien.Visibility = Visibility.Visible;
        //        btnTinhTongCong.Visibility = Visibility.Collapsed;

        //        if (App.IsChapNhanQCBang == true)
        //        {
        //            maQCBangTextBox.Text = App.QcBangTemp.MaQCBang;
        //            tenLoaiBangComboBox.SelectedItem = _selectInfo.SelectTenLoaiBang(App.QcBangTemp.MaLoaiBang);
        //            donGiaBangTextBox.Text = _selectInfo.SelectDonGiaBang(App.QcBangTemp.MaLoaiBang).ToString();
        //            thanhTienBangTextBox.Text = App.QcBangTemp.ThanhTien.ToString();
        //        }

        //        if (App.IsChapNhanQCToBuom == true)
        //        {
        //            maQCToBuomTextBox.Text = App.QcToBuomTemp.MaQCToBuom;
        //            //soLuongComboBox.SelectedItem = App.QcToBuomTemp.SoLuong;
        //            soLuongTextBox.Text = App.QcToBuomTemp.SoLuong.ToString();
        //            donGiaToBuomTextBox.Text = App.QcToBuomTemp.DonGia.ToString();
        //            thanhTienToBuomTextBox.Text = App.QcToBuomTemp.ThanhTien.ToString();
        //        }

        //        if (App.IsChapNhanQCBao == true)
        //        {
        //            maQCBaoTextBox.Text = App.QcBaoTemp.MaQCBao;
        //            toBaoComboBox.SelectedItem = _selectInfo.SelectTenToBao(App.QcBaoTemp.MaToBao);
        //            if (App.QcBaoTemp.CoHinhAnh == 1)
        //                dkChupAnhcheckBox.IsChecked = true;
        //            else dkChupAnhcheckBox.IsChecked = false;
        //            kyBatDauTextBox.Text = App.QcBaoTemp.KyBatDau.ToString();
        //            soKyTextBox.Text = App.QcBaoTemp.SoKy.ToString();
        //            thanhTienBaoTextBox.Text = App.QcBaoTemp.ThanhTien.ToString();
        //        }
        //    }
        //}

        private void kyBatDauTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            _otherHandle.IsNumber(kyBatDauTextBox);
        }

        private void soLuongTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            _otherHandle.IsNumber(soLuongTextBox);
        }

        private void btnTinhTienToBuom_Click(object sender, RoutedEventArgs e)
        {
            if (soLuongTextBox.Text == "")
                MessageBox.Show("Chưa nhập số lượng tờ bướm", "Thông Báo");
            else
            {
                try
                {
                    if (!m_bSelectDataToBuom)
                    {
                        int _iSoluong = int.Parse(soLuongTextBox.Text.ToString());

                        if (_iSoluong < _selectInfo.SelectThamSo("SoToBuomToiThieu "))
                            MessageBox.Show("Số lượng tờ bướm quá ít", "Thông Báo");
                        else
                        {
                            _otherHandle.TinhTienQCToBuom(_iSoluong, ref donGiaToBuomTextBox, ref thanhTienToBuomTextBox);

                            btnChapNhanToBuom.IsEnabled = true;
                        }
                    }
                    else
                        m_bSelectDataToBuom = false;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Thông Báo");
                }
            }
        }

        private void btnHuy_Click(object sender, RoutedEventArgs e)
        {
            ResetAll();
            App.Mainform.btnXacNhanGiayTo.IsEnabled = false;
            App.Mainform.btnLapPhieuThu.IsEnabled = false;
            App.IsDangKyQuangCao = false;
            App.IsChapNhanQCToBuom = false;
            App.IsChapNhanQCBang = false;
            App.IsChapNhanQCBao = false;

            btnChapNhanToBuom.Visibility = Visibility.Visible;
            btnTinhTienToBuom.Visibility = Visibility.Visible;
            btnChapNhanBao.Visibility = Visibility.Visible;
            btnChapNhanBang.Visibility = Visibility.Visible;
        }
    }
}
